package xyz.jfshare.netty.study.server.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import xyz.jfshare.netty.study.server.config.Config;
import xyz.jfshare.netty.study.server.dao.UserMapper;
import xyz.jfshare.netty.study.server.entity.User;
import xyz.jfshare.netty.study.server.service.UserService;
import xyz.jfshare.netty.study.server.util.RandomUtil;


/**
 * @author YuJie_Jin
 * Description: 用户
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public String login(String username, String password) {
        User user = getOne(
                new QueryWrapper<User>()
                        .eq("username",username)
                        .eq("is_delete",0)
        );
        if (user==null){
            return "用户名不存在";
        }
        String salt = user.getSalt();
        String md5Pwd = DigestUtils.md5DigestAsHex((password+salt).getBytes());
        if (!md5Pwd.equals(user.getPassword())){
            return "密码错误";
        }
        if (user.getStatus().equals(Config.BANNED)){
            return "用户已封禁，请联系管理员";
        }
        if (user.getStatus().equals(Config.ONLINE)){
            return "用户已登录，不能重复登录";
        }
        //更新登录信息
        user.setStatus(Config.ONLINE);
        user.setUpdateTime(null);
        boolean res = updateById(user);
        if (!res){
            return "服务器错误,请联系管理员";
        }
        String json = JSONObject.toJSONString(user);
        return "success-" + json;
    }

    @Override
    public String register(String username, String password, String rePassword, String nickName) {
        //判断用户名是否已存在
        User one1 = getOne(
                new QueryWrapper<User>()
                        .eq("is_delete",0)
                        .eq("username",username)
        );
        if (one1 != null){
            return "用户名已经存在,请更换";
        }
        //判断昵称是否已存在
        User one = getOne(
                new QueryWrapper<User>()
                        .eq("is_delete",0)
                        .eq("nick_name",nickName)
        );
        if (one != null){
            return "该昵称已经存在,请更换";
        }
        //判断密码长度是否少于6位
        if (password.length() < 6){
            return "密码不能少于6位";
        }
        //判断两次密码是否一致
        if (!rePassword.equals(password)){
            return "两次密码输入不一致";
        }
        //创建新用户对象
        User user = new User();
        //随机生成盐(6位)
        String salt = RandomUtil.getRandomString(6);
        //设置数据库密码，加盐加密(md5)
        String pwd = DigestUtils.md5DigestAsHex((password+salt).getBytes());
        //设置属性
        user.setNickName(nickName);
        user.setUsername(username);
        user.setSalt(salt);
        user.setPassword(pwd);
        int res = userMapper.insert(user);
        if (res == 0){
            return "服务器错误,请联系管理员";
        }
        return "success";
    }

    @Override
    public boolean setAllOffline() {
        return userMapper.setAllOffline();
    }

    @Override
    public User getByNickName(String nickName) {
        return getOne(
                new QueryWrapper<User>()
                        .eq("is_delete",0)
                        .eq("nick_name",nickName)
        );
    }

    @Override
    public int insert(User user) {
        return userMapper.insert(user);
    }

    @Override
    public boolean setWindow(int id,int window) {
        return userMapper.setWindow(id,window);
    }
}
